package com.twu.biblioteca;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

enum Menu {

    LISTBOOKS("List Of Books", "1") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println(bookCollection.toString());
            return new ViewState(currentUser, true);
        }
    },
    CHECKOUT("Check Out Book", "2") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            currentUser = login(scanner, printStream, userCollection, currentUser);
            if (currentUser == null) {
                return new ViewState(currentUser, true);
            }
            printStream.println("Please enter the name of the book to be checked out");
            String bookName = scanner.next();
            if (bookCollection.checkOut(bookName)) {
                printStream.println("Thank You..Enjoy The Book");
            } else {
                printStream.println("Sorry the Book is not Available");
            }
            return new ViewState(currentUser, true);
        }
    },

    RETURNBOOK("Return Book", "3") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            currentUser = login(scanner, printStream, userCollection, currentUser);
            if (currentUser == null) {
                return new ViewState(currentUser, true);
            }

            printStream.println("Enter the Name Of the Book U want to return");
            String bookName = scanner.next();
            if (bookCollection.checkIn(bookName)) {
                printStream.println("Thank You For Returning The Book");
            } else {
                printStream.println("Sorry You are returning the wrong book");
            }
            return new ViewState(currentUser, true);
        }
    },

    LISTOFMOVIES("List Of Movies", "4") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println(movieCollection.toString());
            return new ViewState(currentUser, true);
        }
    },

    CHECKOUTMOVIE("Check Out A movie", "5") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println("Please enter the name of the Movie to be checked out");
            String movieName = scanner.next();
            if (movieCollection.checkOut(movieName)) {
                printStream.println("Thank You..Enjoy The Movie");
            } else {
                printStream.println("Sorry the Movie is not Available");
            }
            return new ViewState(currentUser, true);
        }
    },

    RETURNMOVIE("Return a Movie", "6") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println("Enter the Name Of the Movie U want to return");
            String movieName = scanner.next();
            if (movieCollection.checkIn(movieName)) {
                printStream.println("Thank You For Returning The Movie");
            } else {
                printStream.println("Sorry You are returning the wrong Movie");
            }

            return new ViewState(currentUser, true);
        }
    },

    INFORMATION("User Information", "7") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            currentUser = login(scanner, printStream, userCollection, currentUser);
            if (currentUser == null) {
                return new ViewState(currentUser, true);
            }
            printStream.println(currentUser);
            return new ViewState(currentUser, true);
        }
    },
    INVALIDOPTION("", "9") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println("Select a Valid Option");
            return new ViewState(currentUser, true);
        }

        @Override
        public String toString() {
            return "";
        }
    },
    QUIT("Quit", "8") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser) {
            return new ViewState(currentUser, false);
        }
    };
    String option;
    String number;

    Menu(String option, String number) {
        this.option = option;
        this.number = number;
    }

    public static Menu find(String id) {
        for (Menu menu : Menu.values()) {
            if (menu.matches(id))
                return menu;
        }
        return Menu.INVALIDOPTION;
    }

    public static void printMenu(PrintStream printStream) {
        for (Menu menu : Menu.values()) {
            printStream.print(menu);
        }
    }

    protected User login(Scanner scanner, PrintStream printStream, UserCollection userCollection, User currentUser) {
        if (currentUser != null) {
            return currentUser;
        }
        printStream.println("Enter Library Number");
        String libraryNo = scanner.next();
        printStream.println("Enter Password");
        String password = scanner.next();
        User foundUser = userCollection.matches(libraryNo, password);
        if (foundUser == null) {
            printStream.println("Invalid Login");
        }
        return foundUser;
    }

    public boolean matches(String otherNumber) {
        return this.number.equals(otherNumber);
    }

    @Override
    public String toString() {
        return String.format("%s.%s\n", number, option);
    }

    public abstract ViewState execute(Scanner scanner, PrintStream printStream, InputStream input, Collection bookCollection, Collection movieCollection, UserCollection userCollection, User currentUser);
}
